package com.xspace.android.imcommon.utils.sk;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 聊天消息表
 * 
 * 
 */
public class ChatMessage extends XmppMessage {

	public ChatMessage() {
	}

	public ChatMessage(String jsonData) {
		parserJsonData(jsonData);
	}

	
	private String fromUserId;

	
	private String fromUserName;// 发送者名称
	private String fromUserLogo;// 发送者头像url

	public String getFromUserLogo() {
		return fromUserLogo;
	}

	public void setFromUserLogo(String fromUserLogo) {
		this.fromUserLogo = fromUserLogo;
	}


	/**
	 * 在不同的消息类型里，代表不同的含义：<br/>
	 * {@link XmppMessage#TYPE_TEXT} 文字 <br/>
	 * {@link XmppMessage#TYPE_IMAGE} 图片的Url<br/>
	 * {@link XmppMessage#TYPE_VOICE} 语音的Url <br/>
	 * {@link XmppMessage#TYPE_LOCATION} 地理<br/>
	 * {@link XmppMessage#TYPE_GIF} Gif图的名称 <br/>
	 * {@link XmppMessage#TYPE_TIP} 系统提示的字<br/>
	 * {@link XmppMessage#TYPE_FILE} 文件的url<br/>
	 */
	
	private String content;

	
	private String location_x;// 当为地理位置时，有效

	
	private String location_y;// 当为地理位置时，有效

	
	private int fileSize;// 当为图片、语音消息时，此节点有效。图片、语音文件的大小

	
	private int timeLen;// 当为语音消息时，此节点有效。语音信息的长度

	/* 本地额外存数数据 */
	private int _id;

	
	private int timeReceive;// 接收到消息回执的时间

	
	private String filePath;// 为语音视频图片文件的 本地路径（IOS端叫fileName），注意本地文件可能清除了，此节点代表的数据不一定有效

	
	private boolean isUpload;// 当为图片和语音类型是，此节点有效，代表是否上传完成，默认false。isMySend=true，此节点有效，

	
	private boolean isDownload;// 当为图片和语音类型是，此节点有效，代表是否下载完成，默认false。isMySend=false,此节点有效

	
	private int messageState;// 只有当消息是我发出的，此节点才有效。消息的发送状态,默认值=0，代表发送中

	// 当为语音文件时，此节点代表语音是否已经读了。当为新朋友推送消息时，代表改推送消息是否已读。只在这两种情况下有效
	
	private boolean isRead;// 默认为false 代表我未读

	
	private int sipStatus;// 语音或者视频通话的状态，本地数据库存储即可

	
	private int sipDuration;// 语音或者视频通话的时间，本地数据库存储即可

	// //////推送特有的//////
	
	private String roomId;// 
	public String getRoomId() {
		return roomId;
	}

	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}


	private String roomName;
	private String platForm;
	public String getPlatForm() {
		return platForm;
	}

	public void setPlatForm(String platForm) {
		this.platForm = platForm;
	}


	private String uuid;

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

	public int get_id() {
		return _id;
	}

	public void set_id(int _id) {
		this._id = _id;
	}

	public String getFromUserId() {
		return fromUserId;
	}

	public void setFromUserId(String fromUserId) {
		this.fromUserId = fromUserId;
	}

	// public String getToUserId() {
	// return toUserId;
	// }
	//
	// public void setToUserId(String toUserId) {
	// this.toUserId = toUserId;
	// }

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getLocation_x() {
		return location_x;
	}

	public void setLocation_x(String location_x) {
		this.location_x = location_x;
	}

	public String getLocation_y() {
		return location_y;
	}

	public void setLocation_y(String location_y) {
		this.location_y = location_y;
	}

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	public int getTimeLen() {
		return timeLen;
	}

	public void setTimeLen(int timeLen) {
		this.timeLen = timeLen;
	}

	public int getMessageState() {
		return messageState;
	}

	public void setRead(boolean isRead) {
		this.isRead = isRead;
	}

	public void setMessageState(int messageState) {
		this.messageState = messageState;
	}

	public boolean isRead() {
		return isRead;
	}


	public String getFromUserName() {
		return fromUserName;
	}

	public void setFromUserName(String fromUserName) {
		this.fromUserName = fromUserName;
	}

	public boolean isUpload() {
		return isUpload;
	}

	public void setUpload(boolean isUpload) {
		this.isUpload = isUpload;
	}

	public boolean isDownload() {
		return isDownload;
	}

	public void setDownload(boolean isDownload) {
		this.isDownload = isDownload;
	}

	public int getTimeReceive() {
		return timeReceive;
	}

	public void setTimeReceive(int timeReceive) {
		this.timeReceive = timeReceive;
	}

	/**
	 * 解析接收到的消息
	 * 
	 * @param jsonData
	 */
	private void parserJsonData(String jsonData) {
		try {
			JSONObject jObject =new JSONObject(jsonData);
			type = getIntValueFromJSONObject(jObject, "type");
			timeSend = getIntValueFromJSONObject(jObject, "timeSend");
			fromUserId = getStringValueFromJSONObject(jObject, "fromUserId");
			fromUserName = getStringValueFromJSONObject(jObject, "fromUserName");
			fromUserLogo = getStringValueFromJSONObject(jObject, "fromUserLogo");
			content = getStringValueFromJSONObject(jObject, "content");
			location_x = getStringValueFromJSONObject(jObject, "location_x");
			location_y = getStringValueFromJSONObject(jObject, "location_y");
			fileSize = getIntValueFromJSONObject(jObject, "fileSize");
			timeLen = getIntValueFromJSONObject(jObject, "timeLen");
            roomId=getStringValueFromJSONObject(jObject, "roomId");
            uuid=getStringValueFromJSONObject(jObject, "uuid");
            roomName=getStringValueFromJSONObject(jObject, "roomName");
            platForm=getStringValueFromJSONObject(jObject, "platForm");
			// 表示未读
			isRead = false;
			isMySend = false;
			isDownload = false;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String toJsonString(boolean isGroupChatMsg)  {
		String msg = "";
		JSONObject object = new JSONObject();
		try {
			object.put("type", this.type);
			object.put("timeSend", this.timeSend);
			if (isGroupChatMsg) {
				object.put("fromUserId", this.fromUserId);
			}

			if (!TextUtils.isEmpty(this.fromUserName)) {
				object.put("fromUserName", this.fromUserName);
			}

			if (!TextUtils.isEmpty(this.content)) {
				object.put("content", this.content);
			}

			if (!TextUtils.isEmpty(this.location_x)) {
				object.put("location_x", this.location_x);
			}

			if (!TextUtils.isEmpty(this.location_y)) {
				object.put("location_y", this.location_y);
			}

			if (!TextUtils.isEmpty(this.roomId)) {
				object.put("roomId", this.roomId);
			}
			if (!TextUtils.isEmpty(this.uuid)) {
				object.put("uuid", this.uuid);
			}
			if (!TextUtils.isEmpty(this.roomName)) {
				object.put("roomName", this.roomName);
			}
			if (!TextUtils.isEmpty(this.platForm)) {
				object.put("platForm", this.platForm);
			}
			if (!TextUtils.isEmpty(this.fromUserLogo)) {
				object.put("fromUserLogo", this.fromUserLogo);
			}
			if (this.fileSize > 0) {
				object.put("fileSize", this.fileSize);
			}

			if (this.timeLen > 0) {
				object.put("timeLen", this.timeLen);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		msg = object.toString();
		return msg;
	}

	public boolean validate() {
		return type != 0 && !TextUtils.isEmpty(fromUserId) && !TextUtils.isEmpty(fromUserName) && timeSend != 0;
	}
}
